﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace EC.Admin.Controllers
{
    using EC.Application.Interface;
    using EC.Entity.Query;
    using EC.Entity.Request.Bank;
    using EC.Entity.Response.Bank;
    using System.IO;
    using System.Net.Http;
    using System.Text;
    using System.Text.Json;

    public class BankController : BaseController
    {

        private readonly string CcdcapiUrl = "https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo={0}&cardBinCheck=true";

        private readonly BankService _bankListService;

        private readonly IHttpClientFactory _httpClientFactory;

        public BankController(BankService bankListService, IHttpClientFactory httpClientFactory, LoginService loginService) : base(loginService)
        {
            _bankListService = bankListService;
            _httpClientFactory = httpClientFactory;
        }

        public IActionResult BankList()
        {
            return View();
        }

        public IActionResult Add()
        {
            return View();
        }


        [HttpGet]
        public async Task<JsonResult> GetBankNameByCard(string cardNo)
        {
            BankResponse bank = new BankResponse();
            var client = _httpClientFactory.CreateClient();
            var request = new HttpRequestMessage(HttpMethod.Get, string.Format(CcdcapiUrl, cardNo));
            var response = await client.SendAsync(request);
            if (response.IsSuccessStatusCode)
            {
                using Stream responseStream = response.Content.ReadAsStreamAsync().Result;
                using StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
                var responseStr = streamReader.ReadToEnd();
                ValidateCardResponse validateCardResponse = JsonSerializer.Deserialize<ValidateCardResponse>(responseStr);
                if (validateCardResponse.Validated && !string.IsNullOrEmpty(validateCardResponse.Bank))
                {
                    bank = _bankListService.GetBankByCode(validateCardResponse.Bank);
                }
            }

            return Json(bank);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<UserResponse></returns>
        public IActionResult BankListQuery(BankQuery query)
        {
            var list = _bankListService.GetPager(query);

            return Json(list);
        }


        public IActionResult EditBank(int id)
        {
            var model = _bankListService.Get(id);

            return View("Add", model);
        }



        [HttpPost]
        public JsonResult SaveBank(SaveBankRequest request)
        {
            request.UserId = UserId;
            var response = _bankListService.SaveBank(request);

            return Json(response);
        }



        [HttpPost]
        public JsonResult DelBank(int Id)
        {
            var response = _bankListService.DelBank(Id);

            return Json(response);
        }
    }
}
